package com.qfedu.web;

import com.qfedu.dao.AccountDao;
import com.qfedu.dao.impl.AccountDaoImpl;
import com.qfedu.pojo.Account;
import com.qfedu.pojo.User;
import com.qfedu.service.AccountService;
import com.qfedu.service.UserService;
import com.qfedu.service.impl.AccountServiceImpl;
import com.qfedu.service.impl.UserServiceImpl;
import com.qfedu.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;
@WebServlet("/userServlet")
public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();
    private AccountDao accountDao = new AccountDaoImpl();
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //  1、获取请求的参数
        String verify = (String) request.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        String verifycode = request.getParameter("verifycode");
        if (!verify.equalsIgnoreCase(verifycode)) {
            request.setAttribute("msg", "验证码输入有误");
            request.getRequestDispatcher("pages/user/login.jsp").forward(request, response);
        } else {
            String phone = request.getParameter("phone");
            String password = request.getParameter("password");
            User user = new User();
            user.setPhone(phone);
            user.setPassword(password);
            User loginUser = userService.login(user);
            if (loginUser == null) {
                request.setAttribute("msg", "用户名或密码输入错误");
                request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
            } else {
                // 登录成功
                //跳到成功页面login_success.html
                request.getSession().setAttribute("user", loginUser);
                Account account = accountDao.queryAccountByPhone(loginUser.getPhone());
                if (account!=null){
                    request.getSession().setAttribute("account",account);
                    request.getRequestDispatcher("/index.jsp").forward(request,response);

                }else {
                    request.getRequestDispatcher("/pages/user/login_success.jsp").forward(request, response);
                }

            }
        }
    }

    public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1、获取请求的参数
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        String code = request.getParameter("verifycode");
        String verify = (String) request.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        User user = new User();
        user.setPhone(phone);
        user.setPassword(password);
        //2、检查验证码是否正确
        if (verify != null && verify.equalsIgnoreCase(code)) {
            //3、检查 用户名是否可用
            if (userService.existsUser(user)) {
                request.setAttribute("msg", "该用户已存在!");
                //跳回注册页面
                request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
            } else {
                userService.registerUser(user);
                request.getRequestDispatcher("/pages/user/regist_success.jsp").forward(request, response);
            }
        } else {
            request.setAttribute("msg", "验证码错误！！");
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
        }
    }

    public void forget(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AccountService accountService = new AccountServiceImpl();

        String phone = req.getParameter("phone");
        String name = req.getParameter("name");
        String idcode = req.getParameter("idcode");
        String password = req.getParameter("password");

        String code = req.getParameter("verifycode");
        String verify = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        //验证码
        if (verify != null && verify.equalsIgnoreCase(code)) {
            //验证个人信息是否匹配
            if (accountService.forgetCheck(name, phone, idcode) != null) {
                //匹配 将新密码存至user
                userService.newPassword(phone, password);
                req.getRequestDispatcher("/pages/user/forget_success.jsp").forward(req, resp);

            } else {
                req.setAttribute("msg", "信息校验有误!");
                //跳回页面
                req.getRequestDispatcher("/pages/user/forget.jsp").forward(req, resp);
            }
        } else {
            System.out.println("验证码[" + code + "]错误");
            //把回显信息，保存到Request域中
            req.setAttribute("msg", "验证码错误！！");
            req.getRequestDispatcher("/pages/user/forget.jsp").forward(req, resp);
        }
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String oldPassword = req.getParameter("oldpassword");//原密码
        String password = req.getParameter("password");//新密码
        //获取手机号
        User loginuser=(User)req.getSession().getAttribute("user");//如何获取
        String phone = loginuser.getPhone();

        String code = req.getParameter("verifycode");
        String verify = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        //验证码
        if (verify != null && verify.equalsIgnoreCase(code)) {
            //验证个人信息是否匹配
            User user = new User();
            user.setPhone(phone);
            user.setPassword(oldPassword);
            User loginUser = userService.login(user);
            if (loginUser != null) {
                //匹配 将新密码存至user
                userService.newPassword(phone, password);
                req.getRequestDispatcher("/pages/user/updatepassword_success.jsp").forward(req, resp);
            } else {
                req.setAttribute("msg", "原密码错误!");
                //跳回页面
                req.getRequestDispatcher("/pages/user/updatepassword.jsp").forward(req, resp);
            }
        }else {
            System.out.println("验证码[" + code + "]错误");
            //把回显信息，保存到Request域中
            req.setAttribute("msg", "验证码错误！！");
            req.getRequestDispatcher("/pages/user/updatepassword.jsp").forward(req, resp);
        }
    }
    public void fulluser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute("user");
        if (user!=null){
            req.getRequestDispatcher("/pages/user/fulluser.jsp").forward(req,resp);
        }else {
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
        }
    }
    public void isFullUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute("user");
        String phone = user.getPhone();
        String name = req.getParameter("name");
        String idcode = req.getParameter("idcode");
        String gender = req.getParameter("gender");
        String nowCity = req.getParameter("nowCity");
        String nowProvince = req.getParameter("nowProvince");
        String address = req.getParameter("address");
        StringBuilder stringBuilder = new StringBuilder();
        String location = stringBuilder.append(nowProvince).append(nowCity).append(address).toString();
        int i = userService.fullUser(phone,name,gender,idcode,location);
        req.getRequestDispatcher("/pages/user/fullUser_success.jsp").forward(req,resp);

    }

}
